// #define print(...)

class Solution {
    static constexpr int mod = 1'000'000'007;
public:
int minAbsoluteSumDiff(vector<int>& nums1, vector<int>& nums2) {
    auto rec(nums1);
    sort(rec.begin(), rec.end());
    int diffsum = 0, maxChg = 0;
    for(int i=0; i<nums1.size(); i++){
        auto diff = abs(nums1[i] - nums2[i]);
        diffsum = (diffsum + diff) % mod;
        auto lb = lower_bound(rec.begin(), rec.end(), nums2[i]);
        if(rec.end() != lb) // 0 ~ end()-1
            maxChg = max(maxChg, diff - (*lb - nums2[i]));
        if(rec.begin() < lb) // 1 ~ end()
            maxChg = max(maxChg, diff - (nums2[i] - *(lb-1)));
    }
    return (diffsum - maxChg + mod) % mod;
}
};